function padDate(num) {
  return num >= 10 ? num : '0' + num
}

export function formatDate(val) {
  let value = new Date(val);
  let  year=value.getFullYear();
  let month=padDate(value.getMonth()+1);
  let day=padDate(value.getDate());
  let hour=padDate(value.getHours());
  let minutes=padDate(value.getMinutes());
  let seconds=padDate(value.getSeconds());
  return year + '/' + month + '/' + day + ' ' + hour + ':' + minutes + ':' + seconds;
}
export function formatDateNoYear(val) {
  let value = new Date(val);
  let year=value.getFullYear();
  let month=padDate(value.getMonth()+1);
  let day=padDate(value.getDate());
  let hour=padDate(value.getHours());
  let minutes=padDate(value.getMinutes());
  let seconds=padDate(value.getSeconds());
  return month + '/' +day + ' ' + hour + ':' + minutes + ':' + seconds;
}

export function formatDateYMD(val) {
  let value = new Date(val);
  let year = value.getFullYear().toString().slice(2);
  let month = padDate(value.getMonth()+1);
  let day = padDate(value.getDate());
  return  year + '/' + month + '/' + day
}
export function formatDateHMS(val) {
  let value = new Date(val);
  let hour = padDate(value.getHours());
  let minutes = padDate(value.getMinutes());
  let seconds = padDate(value.getSeconds());
  return  hour + ':' + minutes + ':' + seconds
}


export function formatCountDown(timestamp) {
  let current = new Date().getTime()
  let timeDiff = (Number(timestamp) - Number(current)) / 1000
  if (timeDiff <= 0) {
    return '00:00:00'
  }
  let hour = parseInt(timeDiff / 3600 % 24)
    hour =  padDate(hour)
  let minute = parseInt(timeDiff / 60 % 60)
    minute =  padDate(minute)
  let second = parseInt(timeDiff  % 60 ) 
    second =  padDate(second)
  let time = `${hour}:${minute}:${second}`
  // console.log(time)
  return time
}

export function isPhone() {
  var width = document.documentElement.clientWidth||document.body.clientWidth
  return width < 750
}

export function getByteLen(val) {
  let len = 0
  for (var i = 0; i < val.length; i++) {
    var a = val.charAt(i);
    if (a.match(/[^\x00-\xff]/ig) != null) {
      len += 2
    }
    else {
      len += 1
    }
  }
  return len
}

export function sumArr(arr) {
  let s = 0
  for(let i = 0; i < arr.length; i++) {
    s += parseInt(arr[i])
  }
  return s 
}

